
clear
set more off

cd "D:\data_replication"

import delimited using estimation\4_demand_estimation\4_cobb_douglas_weights\Y_summary_8d.csv


// Format Data
//==============================================================================

// Normalize Incomes
//------------------------------------------------------------------------------

egen Y_min = min(y_i_summary57)
forval i = 5/104 {
   replace y_i_summary`i' = y_i_summary`i' - Y_min
}

egen Y_max = max(y_i_summary46)
forval i = 5/104 {
   replace y_i_summary`i' = y_i_summary`i'/Y_max
}
save estimation\4_demand_estimation\4_cobb_douglas_weights\Y_market.dta, replace


// Create Income Ratios and save
//------------------------------------------------------------------------------

gen sum_Y = 0
forval i = 5/104 {
   replace sum_Y = sum_Y + y_i_summary`i'
}

gen sum_Y2 = 0
forval i = 5/104 {
   replace sum_Y2 = sum_Y2 + (y_i_summary`i')^2
}
   
   
gen sum_Y2_Y = sum_Y2 / sum_Y

keep pc8 year quarter declarant share_pc8 sum_Y2_Y 
   
save estimation\4_demand_estimation\4_cobb_douglas_weights\reg_temp.dta, replace


// Create Data for Outside category, merge, and prepare regressions
//==============================================================================

keep if pc8 == 0
rename share_pc8 share_pc8_out
keep year quarter declarant share_pc8_out
save estimation\4_demand_estimation\4_cobb_douglas_weights\share_out.dta, replace
  
   
use estimation\4_demand_estimation\4_cobb_douglas_weights\reg_temp.dta, clear
merge m:1 year quarter declarant using estimation\4_demand_estimation\4_cobb_douglas_weights\share_out.dta

drop if pc8 == 0
gen s_ratio = share_pc8/share_pc8_out

replace sum_Y2_Y = sum_Y2_Y / share_pc8_out
gen reg_const = 1 / share_pc8_out


egen pc8_group = group(pc8)
save estimation\4_demand_estimation\4_cobb_douglas_weights\pc8_groups.dta, replace

replace pc8 = pc8_group


// Regressions
//==============================================================================

levelsof pc8, local(levels) 
foreach i of local levels {
regress s_ratio reg_const sum_Y2_Y if pc8 == `i', noconstant
estout using estimation\4_demand_estimation\4_cobb_douglas_weights\res_`i'.txt, cells("b se t p") replace
}


levelsof pc8, local(levels) 
foreach i of local levels {

import delimited estimation\4_demand_estimation\4_cobb_douglas_weights\res_`i'.txt, clear
keep if v1 == "reg_const"
keep v2
rename v2 c0
gen pc8 = `i'
order pc8 c0
save estimation\4_demand_estimation\4_cobb_douglas_weights\res_c0_`i'.dta, replace

import delimited estimation\4_demand_estimation\4_cobb_douglas_weights\res_`i'.txt, clear
keep if v1 == "sum_Y2_Y"
keep v2
rename v2 c1
gen pc8 = `i'
order pc8 c1
save estimation\4_demand_estimation\4_cobb_douglas_weights\res_c1_`i'.dta, replace

rm estimation\4_demand_estimation\4_cobb_douglas_weights\res_`i'.txt

}


// Collect Results in single file
//==============================================================================

use estimation\4_demand_estimation\4_cobb_douglas_weights\res_c0_1.dta, clear

foreach i of local levels {

append using estimation\4_demand_estimation\4_cobb_douglas_weights\res_c0_`i'.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\res_c0_`i'.dta

}

drop if _n == 1
destring c0, replace
save estimation\4_demand_estimation\4_cobb_douglas_weights\results_c0.dta, replace


use estimation\4_demand_estimation\4_cobb_douglas_weights\res_c1_1.dta, clear

foreach i of local levels {

append using estimation\4_demand_estimation\4_cobb_douglas_weights\res_c1_`i'.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\res_c1_`i'.dta

}

drop if _n == 1
destring c1, replace
save estimation\4_demand_estimation\4_cobb_douglas_weights\results_c1.dta, replace


use estimation\4_demand_estimation\4_cobb_douglas_weights\results_c0.dta, clear
merge 1:1 pc8 using estimation\4_demand_estimation\4_cobb_douglas_weights\results_c1.dta
drop _merge
save estimation\4_demand_estimation\4_cobb_douglas_weights\results_c0_c1.dta, replace


// Translate results by group back to pc8plus
//==============================================================================

use estimation\4_demand_estimation\4_cobb_douglas_weights\pc8_groups.dta, clear
keep pc8 pc8_group
sort pc8_group
by pc8_group: keep if _n == 1
save estimation\4_demand_estimation\4_cobb_douglas_weights\pc8_groups.dta, replace


use estimation\4_demand_estimation\4_cobb_douglas_weights\results_c0_c1.dta, clear
rename pc8 pc8_group
merge 1:1 pc8_group using estimation\4_demand_estimation\4_cobb_douglas_weights\pc8_groups.dta
drop _merge
order pc8_group pc8 c0 c1
save estimation\4_demand_estimation\4_cobb_douglas_weights\results_c0_c1.dta, replace


// Merge Income and Result Data
//==============================================================================

use estimation\4_demand_estimation\4_cobb_douglas_weights\Y_market.dta, clear
merge m:1 pc8 using estimation\4_demand_estimation\4_cobb_douglas_weights\results_c0_c1.dta
drop Y_min Y_max _merge pc8_group
export delimited using estimation\4_demand_estimation\4_cobb_douglas_weights\results_c0_c1.csv, replace


// Clean up
//==============================================================================

rm estimation\4_demand_estimation\4_cobb_douglas_weights\results_c0.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\results_c1.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\reg_temp.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\share_out.dta
rm estimation\4_demand_estimation\4_cobb_douglas_weights\pc8_groups.dta

